package project.controllers;

import java.util.Scanner;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;

import api.messages.Messager;


public class ConsoleScanner {
	
	private  final String CHECK_IS_DIGIT_REGEXP = "^-?\\d+$";
	private Scanner reader = new Scanner(System.in);
	private Integer default_number=1;
	private  final Logger log = Logger.getLogger(ConsoleScanner.class);
	private static ConsoleScanner CONSOLE_SCANNER;
	private static final String INPUT_ERROR="Input error. Letters or wrong number";
	
    private ConsoleScanner(){
    	
		
	}
	
	public static  ConsoleScanner getInstance(){
		if(CONSOLE_SCANNER == null){
			CONSOLE_SCANNER = new ConsoleScanner();
		}
		return CONSOLE_SCANNER;
	}

	
	public  Integer getChoice(){	
		String s=reader.nextLine();
		if(Pattern.matches(CHECK_IS_DIGIT_REGEXP, s)){
			Integer choise = Integer.parseInt(s);
			return choise;
		}
		log.info(INPUT_ERROR);
		throw new RuntimeException();
    }
	
	public  String getData(){
		return reader.nextLine();
	}
	
	public  Integer getIntData(){
		String s=reader.nextLine();
		if(Pattern.matches(CHECK_IS_DIGIT_REGEXP, s)){
			default_number = Integer.parseInt(s);
			return default_number;
		}
		 log.info(INPUT_ERROR);
		Printer.printMessage(Messager.WRONG_DATA_FORMAT);
			return default_number;
	}

}
